// ----------------------------------
// RSDK Project: Sonic 1/Sonic 2
// Script Description: Eggmobile Object
// Script Author: Christian Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

//-------Aliases-------//
private alias 81 : TYPE_EGGMOBILE


// Static Values

// Tables

event ObjectMain
	switch object.state
	case 0
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			BoxCollisionTest(C_BOX, object.entityPos, -24, -96, 24, 96, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
		next
		break
	case 1
		object.priority = PRIORITY_ACTIVE
		object.animation = 2
		object.value0 = 1
		object.yvel = -0x18000
		object.state++
		break
	case 2
		object.ypos += object.yvel
		object.value4++
		if object.value4 >= 52
			object.value2 = 1
			object.xvel = 0x18000
			object.yvel = -0x1800
			object.state++
		end if
		break
	case 4
		object.value5++
		temp0 = object.value5
		temp0 &= 7
		if temp0 == 4
			Rand(temp0, 96)
			temp0 -= 48
			temp0 <<= 16
			temp0 += object.xpos
			Rand(temp1, 48)
			temp1 -= 24
			temp1 <<= 16
			temp1 += object.ypos
			CreateTempObject(TypeName[Explosion], 0, temp0, temp1)
			object[tempObjectPos].drawOrder = 4
			if object.value7 < 256
				PlaySfx(SfxName[Explosion], 0)
			end if
		end if
		if object.value7 < 256
			object.value4++
			if object.value4 >= 30
				PlaySfx(SfxName[Boss Hit], 0)
				object.yvel = 0x6000
				object.value4 = 0
			end if
		end if
	case 3
		object.xpos += object.xvel
		object.ypos += object.yvel
		temp0 = stage.newXBoundary2
		temp0 += 212
		temp0 <<= 16
		if object.xpos >= temp0	
			if object[0].value37 == 5
				recGoggles = 5
			else
				recGoggles = 0
			end if
			if options.gameMode == 2
				recGoggles = 0
				if stage.timeEnabled == 1
					//object.type = TypeName[Blank Object]
					object.state = 6
					ResetObjectEntity(30, TypeName[Act Finish], 0, 0, 0)
					object[30].drawOrder = 6
					object[30].priority = PRIORITY_ACTIVE
					timeAttack.result = stage.seconds
					timeAttack.result *= 100
					temp0 = stage.minutes
					temp0 *= 0x1770
					timeAttack.result += temp0
					timeAttack.result += stage.milliSeconds
					//stage.listPos--
					switch stage.playerListPos
						case 0
							arrayPos0 = 500
							break
						case 1
							arrayPos0 = 530
							break
						case 2
							arrayPos0 = 560
							break
						case 5
							arrayPos0 = 470
							break		
						case 6
							arrayPos0 = 3700
							break						
						case 7
							arrayPos0 = 3750
							break						
						case 8
							arrayPos0 = 3790
							break						
						case 9
							arrayPos0 = 3830
							break													
					end switch
					if SuperTimeAttack == 1
						arrayPos0 = 590
					end if
					arraypos0 += stage.listPos
					if timeAttack.result < saveRAM[arrayPos0]
						saveRAM[arrayPos0] = timeAttack.result
						//object[9].value2 = timeAttack.result
						PlaySfx(SfxName[Event], 0)
					end if
					WriteSaveRAM()				
					PlayMusic(1)
				end if
				stage.timeEnabled = 0
			else
				object.value7 += 8
				music.volume -= 2
				SetClassicFade(0, 0, 0, object.value7)
				if object.value7 >= 384
					if options.gameMode == 1
						arrayPos1 = options.saveSlot
						arrayPos1 <<= 3
						arrayPos1 += 1000
						arrayPos1 += 4
						saveRAM[arrayPos1] = 20
					end if
					stage.activeList = PRESENTATION_STAGE
					stage.listPos = StageName[P - ENDING 1]
					recAnimation = 0
					if options.gameMode == 3
						saveRAM[630] = player.score
						temp0 = stage.playerListPos
						temp0++
						saveRAM[640] = temp0
						stage.listPos = 6
					end if
					WriteSaveRAM()
					LoadStage()
				end if
			end if
		end if
		break
	case 5
		object.xpos = object[+10].xpos
		object.ypos = object[+10].ypos
		if object[+1].type == TypeName[Blank Object]
			object.type = TypeName[Blank Object]
		end if
		break
	end switch
	if object.state == 3
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			CheckEqual(object[currentPlayer].state, PlayerObject_HandleMonikaTeleport)
			temp7 = checkResult
			CheckLower(object[0].value1, 20)
			temp7 &= checkResult
			if temp7 == 0	
				BoxCollisionTest(C_TOUCH, object.entityPos, -23, -16, 23, 16, currentPlayer, object[currentPlayer].value40, object[currentPlayer].value38, object[currentPlayer].value41, object[currentPlayer].value39)
				if checkResult == 0
					BoxCollisionTest(C_TOUCH, object.entityPos, -16, -21, 16, 21, currentPlayer, object[currentPlayer].value40, object[currentPlayer].value38, object[currentPlayer].value41, object[currentPlayer].value39)			
				end if	
				if checkResult == 1
					CallFunction(PlayerObject_CheckHit)
				else
					CallFunction(BossTarget)
				end if		
				if checkResult == 1
					object.state = 4
					object.animation = 3
					PlaySfx(SfxName[Boss Hit], 0)
					object.value4 = 0
					if stage.debugMode == 0
						if currentPlayer == 0
							achievement.trigger = 11
							//CallNativeFunction2(SetAchievement, 10, 100)
						end if
					end if
				end if			
			end if	
			if object[currentPlayer].state == PlayerObject_HandleMonikaTeleport
				if object[0].value1 >= 19
					BoxCollisionTest(C_TOUCH, object.entityPos, -23, -16, 23, 16, currentPlayer, object[currentPlayer].value40, object[currentPlayer].value38, object[currentPlayer].value41, object[currentPlayer].value39)
					if checkResult == 0
						BoxCollisionTest(C_TOUCH, object.entityPos, -16, -21, 16, 21, currentPlayer, object[currentPlayer].value40, object[currentPlayer].value38, object[currentPlayer].value41, object[currentPlayer].value39)			
					end if	
					if checkResult == 1
						object[currentPlayer].state = PlayerObject_HandleAir
						object[currentPlayer].animation = ANI_JUMPING
					end if
				end if
			end if
		next
	end if
	foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
		if object.value6 > 0
			if object[currentPlayer].xpos >= object.value6
				object[currentPlayer].value35 = 0
				object[currentPlayer].value15 = 0
				
				CheckEqual(object[currentPlayer].state, PlayerObject_HandleGround)
				temp0 = checkResult
				CheckEqual(object[currentPlayer].state, PlayerObject_HandleAir)
				temp0 |= checkResult
				CheckEqual(object[currentPlayer].state, PlayerObject_HandleRoll)
				temp0 |= checkResult
				CheckEqual(object[currentPlayer].state, PlayerObject_RollingJump)
				temp0 |= checkResult
				CheckEqual(object[currentPlayer].state, PlayerObject_Blank)
				temp0 |= checkResult
				CheckEqual(object[currentPlayer].state, PlayerObject_HandleYuriBladeAir)
				temp0 |= checkResult
				CheckEqual(object[currentPlayer].state, PlayerObject_HandleYuriBladeGround)
				temp0 |= checkResult
				if temp0 == 1
					object[currentPlayer].state = PlayerObject_Blank
					object[currentPlayer].direction = FLIP_NONE
					object[currentPlayer].controlMode = -1
					if object[currentPlayer].gravity == GRAVITY_GROUND
						object[currentPlayer].xvel = 0
						object[currentPlayer].speed = 0
						if object.state == 3
							object[currentPlayer].animation = ANI_LOOKINGUP
						else
							object[currentPlayer].animation = ANI_STOPPED
						end if
					else
						object[currentPlayer].xvel /= 2
						object[currentPlayer].speed = object[currentPlayer].xvel
						object[currentPlayer].yvel += 0x3800
					end if
				else
					object[currentPlayer].controlMode = -1
					if object[currentPlayer].speed > 0
						object[currentPlayer].speed /= 2
					end if
					if object[currentPlayer].state == PlayerObject_KnuxGlideRight
						object[currentPlayer].value1 = 0
						object[currentPlayer].animation = ANI_GLIDING_DROP
						object[currentPlayer].state = PlayerObject_KnuxGlideDrop
					end if
					if object[currentPlayer].state == PlayerObject_HandleTailsFlight
						object[currentPlayer].state = PlayerObject_HandleAir
						object[currentPlayer].animation = ANI_JUMPING
						object[currentPlayer].yvel = 1
					end if
					if object[currentPlayer].state == PlayerObject_KnuxGlideSlide
						object[currentPlayer].speed = 0
					end if
					if object[currentPlayer].state == PlayerObject_HandleMonikaTeleport
						object.value21++
						if object.value21 == 1
							object[currentPlayer].value1 = 19
						end if
					end if
				end if
			end if
		end if
	next
end event


event ObjectDraw
	switch object.animation
	case 0
		DrawSpriteFX(16, FX_FLIP, object.xpos, object.ypos)
		DrawSpriteFX(17, FX_FLIP, object.xpos, object.ypos)
		break
	case 1
		DrawSpriteFX(0, FX_FLIP, object.xpos, object.ypos)
		break
	case 2
		temp0 = object.animationTimer
		temp0 >>= 2
		temp0 += 1
		object.animationTimer++
		object.animationTimer &= 7
		DrawSpriteFX(temp0, FX_FLIP, object.xpos, object.ypos)
		break
	case 3
		temp0 = object.animationTimer
		temp0 >>= 2
		temp0 += 3
		object.animationTimer++
		object.animationTimer &= 7
		DrawSpriteFX(temp0, FX_FLIP, object.xpos, object.ypos)
		break
	end switch
	switch object.value0
	case 0
		DrawSpriteFX(5, FX_FLIP, object.xpos, object.ypos)
		break
	case 1
		object.value1++
		if object.value1 >= 60
			object.value0 = 2
		else
			temp0 = object.value1
			temp0 /= 20
			temp0 += 5
			DrawSpriteFX(temp0, FX_FLIP, object.xpos, object.ypos)
		end if
		break
	case 2
		break
	end switch
	if object.value2 != 0
		temp0 = object.value3
		temp0 /= 3
		temp0 += 8
		object.value3++
		if object.value3 > 21
			object.value3 = 18
		end if
		DrawSpriteFX(temp0, FX_FLIP, object.xpos, object.ypos)
	end if
end event


event ObjectStartup
	if stage.playerListPos == 2
		if saveRAM[279] == 1
			LoadSpriteSheet("Global/Eggrobo.gif")
		else
			LoadSpriteSheet("Global/Eggman.gif")
		end if
	else
		LoadSpriteSheet("Global/Eggman.gif")
	end if
	SpriteFrame(-28, -32, 64, 21, 131, 172)
	SpriteFrame(-28, -32, 64, 56, 131, 58)
	SpriteFrame(-28, -32, 64, 56, 66, 1)
	SpriteFrame(-28, -32, 64, 56, 1, 172)
	SpriteFrame(-28, -32, 64, 56, 66, 172)
	SpriteFrame(-20, 16, 40, 21, 131, 115)
	SpriteFrame(-20, 8, 48, 19, 172, 115)
	SpriteFrame(-20, 8, 40, 16, 172, 135)
	SpriteFrame(34, 1, 10, 9, 196, 2)
	SpriteFrame(33, 0, 15, 11, 196, 12)
	SpriteFrame(34, -1, 22, 14, 213, 1)
	SpriteFrame(34, -8, 32, 26, 213, 16)
	SpriteFrame(34, -1, 22, 14, 213, 1)
	SpriteFrame(34, -8, 32, 26, 213, 16)
	SpriteFrame(34, 1, 10, 9, 196, 2)
	SpriteFrame(33, 0, 15, 11, 196, 12)
	SpriteFrame(-28, -32, 27, 21, 131, 172)
	SpriteFrame(-28, -11, 64, 35, 131, 193)
	foreach (TypeName[Eggmobile], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].direction = FLIP_X
		object[arrayPos0].priority = PRIORITY_ACTIVE
		if object[arrayPos0].propertyValue == 1
			object[arrayPos0].state = 5
			object[arrayPos0].animation = 1
			object[arrayPos0].value0 = 2			
		end if
	next
end event

event RSDKDraw
	DrawSprite(0)
end event

event RSDKLoad
	LoadSpriteSheet("Global/Display.gif")
	SpriteFrame(-16, -16, 32, 32, 1, 143)
end event
